package com.miplus.ccrm.core.service.aiqm;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.core.controller.vo.aiqm.QmModelRulesResVo;
import com.miplus.ccrm.core.pojo.QmModelRulesLink;
import com.miplus.ccrm.core.mapper.QmModelRulesLinkMapper;
import com.miplus.ccrm.core.pojo.QmQaMain;
import com.miplus.ccrm.core.pojo.QmQaSub;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 20144
 * @description 针对表【QM_MODEL_RULES_LINK(模板关联的大模型质检规则表)】的数据库操作Service实现
 * @createDate 2025-08-20 13:26:42
 */
@Slf4j
@Service
public class QmModelRulesLinkServiceImpl extends ServiceImpl<QmModelRulesLinkMapper, QmModelRulesLink>
        implements QmModelRulesLinkService {
    @Resource
    private QmQaMainService qmQaMainService;
    @Resource
    private QmQaSubService qmQaSubService;

    @Override
    public List<QmModelRulesResVo> qmModelRulesLinkService(Integer tempid) {
        // 1.根据模板id查询大模型质检主项规则关联表，查询模板关联的主项集合
        List<QmModelRulesLink> qmModelRulesLinkList = this.getBaseMapper().selectList(new QueryWrapper<QmModelRulesLink>().eq("templateid", tempid));
        if (qmModelRulesLinkList == null || qmModelRulesLinkList.isEmpty()) {
            return Collections.emptyList();
        }
        // 2.根据主项id，查询主项的名称和评分方案，以及子项集合(主项表字段qasubid存的是子项的数组格式，需要根据数组查询子项)
        return qmModelRulesLinkList.stream().map(qmModelRulesLink -> {
            // 查询主项信息
            QmQaMain qmQaMain = qmQaMainService.getById(qmModelRulesLink.getQamainsid());
            if (qmQaMain == null) {
                return null;
            }
            QmModelRulesResVo qmModelRulesResVo = new QmModelRulesResVo();
            qmModelRulesResVo.setQamainsid(qmQaMain.getQamainsid().intValue());
            qmModelRulesResVo.setQamainname(qmQaMain.getQamainname());
            qmModelRulesResVo.setAssessscheme(qmQaMain.getAssessscheme());
            qmModelRulesResVo.setCrdt(qmQaMain.getCrdt());
            qmModelRulesResVo.setMdusr(qmQaMain.getMdusr());
            qmModelRulesResVo.setMddt(qmQaMain.getMddt());
            // 解析子项ID列表
            String qasubids = qmQaMain.getQasubid();
            if (StrUtil.isNotBlank(qasubids)) {
                try {
                    List<Integer> qasubidList = JSONUtil.parseArray(qasubids).toList(Integer.class);
                    // 查询子项详细信息
                    if (CollUtil.isNotEmpty(qasubidList)) {
                        List<QmModelRulesResVo.QmModelRulesSubResVo> subResVoList = qasubidList.stream().map(subId -> {
                            QmQaSub qmQaSub = qmQaSubService.getById(subId);
                            if (qmQaSub == null) {
                                return null;
                            }
                            QmModelRulesResVo.QmModelRulesSubResVo subResVo = new QmModelRulesResVo.QmModelRulesSubResVo();
                            subResVo.setQasubid(qmQaSub.getQasubid().intValue());
                            subResVo.setQasubname(qmQaSub.getQasubname());
                            subResVo.setMaxscore(qmQaSub.getMaxscore());
                            subResVo.setMinscore(qmQaSub.getMinscore());
                            subResVo.setQmrole(qmQaSub.getQmrole());
                            subResVo.setRemark(qmQaSub.getRemark());
                            subResVo.setAssessscheme(qmQaSub.getAssessscheme());
                            subResVo.setQmrange(qmQaSub.getQmrange());
                            subResVo.setMustcontain(qmQaSub.getMustcontain());
                            subResVo.setMustnotcontain(qmQaSub.getMustnotcontain());
                            subResVo.setCrdt(qmQaSub.getCrdt());
                            subResVo.setMdusr(qmQaSub.getMdusr());
                            subResVo.setMddt(qmQaSub.getMddt());
                            return subResVo;
                        }).filter(Objects::nonNull).collect(Collectors.toList());
                        qmModelRulesResVo.setQasubrules(subResVoList);
                        // 计算总分，增加空值的检查,各个子规则的总分汇总
                        if (CollUtil.isNotEmpty(subResVoList)) {
                            double totalScore = subResVoList.stream()
                                    .filter(Objects::nonNull)
                                    .mapToDouble(subRule -> subRule.getMaxscore() != null ? subRule.getMaxscore().doubleValue() : 0.0)
                                    .sum();
                            qmModelRulesResVo.setTotalscore(BigDecimal.valueOf(totalScore));
                        } else {
                            qmModelRulesResVo.setTotalscore(BigDecimal.ZERO);
                        }
                    }
                    // 如果没有子项，qasubrules将保持为null或空列表，但主项信息仍会显示
                } catch (Exception e) {
                    log.error("解析子项ID列表失败: {}", qasubids, e);
                }
            }
            // 即使没有子项，也会返回包含主项信息的Vo对象
            return qmModelRulesResVo;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

}




